﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Galaktika.BI.Silverlight.Controls
{
    using Galaktika.BI.Runtime;
    using Galaktika.BI.Runtime.Services;
    using Galaktika.BI.Silverlight.Services;

    public class Browser : HtmlHostControl, IBindableObject, IEditableObject
    {
        public Browser()
        {
            //base.LayoutUpdated += new EventHandler(Browser_LayoutUpdated);
        }

        //void Browser_LayoutUpdated(object sender, EventArgs e)
        //{
            //if (this.HtmlElement != null)
            //{
                //this.HtmlElement.SetStyleAttribute("display", "visible");
                //this.HtmlElement.SetStyleAttribute("width", string.Format("{0}px", Convert.ToInt32(this.ActualWidth)));
                //this.HtmlElement.SetStyleAttribute("height", string.Format("{0}px", Convert.ToInt32(this.ActualHeight)));
            //}
        //}

        public string AHtmlText { get; set; }

        public string AUrl { get; set; }

        public bool AShowUrl { get; set; }

        public virtual void BeginEdit()
        {
        }

        public virtual void CancelEdit()
        {
        }

        public virtual void EndEdit()
        {
            IExecutionContext exe = (IExecutionContext)this.Context.Services.GetService(typeof(IExecutionContext));
            if (exe != null)
            {
                
                if (string.IsNullOrEmpty(this.AHtmlText))
                {
                    if (ScriptEngine.ContainsExpression(this.AUrl))
                    {
                        IUserTaskManager taskMgr = (IUserTaskManager)this.Context.Services.GetService(typeof(IUserTaskManager));
                        if (taskMgr != null)
                        {
                            taskMgr.RunTaskAsync(
                                string.Format(Galaktika.BI.Silverlight.Localization.AppTask_ProcessingUrlForControl, base.Name),
                                state =>
                                {
                                    var parsedUrl = exe.Parse(this.Context, this.AUrl);
                                    taskMgr.Dispatcher.BeginInvoke(() => base.ShowHtmlByUri(new Uri(parsedUrl)));
                                });
                        }
                    }
                    else
                    {
                       base.ShowHtmlByUri(new Uri(this.AUrl));
                    }
                }
                else
                {
                    if (ScriptEngine.ContainsExpression(this.AHtmlText))
                    {
                        IUserTaskManager taskMgr = (IUserTaskManager)this.Context.Services.GetService(typeof(IUserTaskManager));
                        if (taskMgr != null)
                        {
                            taskMgr.RunTaskAsync(
                                string.Format(Galaktika.BI.Silverlight.Localization.AppTask_ProcessingUrlForControl, base.Name),
                                state =>
                                {
                                    var parsedHtml = exe.Parse(this.Context, this.AHtmlText);
                                    taskMgr.Dispatcher.BeginInvoke(() => base.ShowHtmlBySource(parsedHtml));
                                });
                        }
                    }
                    else
                    {
                        base.ShowHtmlBySource(this.AHtmlText);
                        //base.HtmlElement.SetStyleAttribute("overflow", "auto");
                    }
                }
            }
        }

        public IBindingContext Context { get; set; }

        private IDictionary<string, IBindingManager> m_Bindings;
        public IDictionary<string, IBindingManager> Bindings
        {
            get
            {
                if (m_Bindings == null)
                {
                    m_Bindings = new SafeDictionary<string, IBindingManager>();
                }

                return m_Bindings;
            }
        }
    }
}
